package org.sluggard.tangela.core;

import java.awt.Color;
import java.awt.event.KeyAdapter;

import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class InputText extends JTextArea {
	
	static final int maxLength = 140;
	
	public InputText() {
		super();
		setColumns(20);
        setRows(5);
        setLineWrap(true);
        MyDocumentListener listener = new MyDocumentListener();
        getDocument().addDocumentListener(listener);
        addKeyListener(listener);
	}
	
	private class MyDocumentListener extends KeyAdapter implements DocumentListener {

		@Override
		public void insertUpdate(DocumentEvent e) {
			DocumentChanged(e);
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			DocumentChanged(e);
		}

		@Override
		public void changedUpdate(DocumentEvent e) {
			
		}
		
		private void DocumentChanged(DocumentEvent e) {
			int length = maxLength - e.getDocument().getLength();
			Container.getMainSelf().getCountLabel().setText(String.valueOf(length));
			if(length >= 0) {
				Container.getMainSelf().getCountLabel().setForeground(Color.BLACK);
			} else {
				Container.getMainSelf().getCountLabel().setForeground(Color.RED);
			}
		}
		
	}

}
